package com.example.springboot_blog.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot_blog.dao.ArticleDao;
import com.example.springboot_blog.domain.Article;
import com.example.springboot_blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author 白草风归
 */
@Service

//@CacheConfig(cacheNames = "article")
public class ArticleServiceImpl extends ServiceImpl<ArticleDao, Article> implements ArticleService, Serializable {

    private static final long serialVersionUID = -1;

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    ArticleServiceImpl articleService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Pram:a
     * @Retrrn:a 根据ID查询，用于文章详情显示
     * redis缓存
     */
    @Override
    public Article getById(Integer id) {
        Object object = redisTemplate.opsForValue().get("article" + id);
        if (object != null) {
            return (Article) object;
        } else {
            Optional<Article> optional = Optional.ofNullable(articleDao.selectById(id));
            if (optional.isPresent()) {
                Article article = optional.get();
                redisTemplate.opsForValue().set("article"+id, article, 1, TimeUnit.DAYS);
                return optional.get();
            } else {
                return null;
            }
        }
    }

    /**
     * @Pram:a
     * @Retrrn:a 分页展示，用于前台展示文章
     */
    @Override
    public Page<Article> getPage(int current, int pageSize) {
        Page<Article> page = new Page<Article>(current, pageSize);
        return articleService.page(page);
    }

    /**
     * @Pram:a
     * @Retrrn:a 查询所有文章，用于后台文章管理（后期可实现分页、排序等功能）
     */
    @Override
    public List<Article> selectList(Article article) {
        return articleDao.selectList(null);
    }

    /**
     * @Pram:a
     * @Retrrn:a 根据ID删除文章
     */
    @Override
    public Boolean delete(Integer id) {
        return articleDao.deleteById(id) > 0;
    }

    /**
     * @return
     * @Pram:a
     * @Retrrn:a 修改文章
     */
    @Override
    public int updateById(Integer id, String title, String body) {
        Article article = new Article();
        article.setId(id);
        article.setTitle(title);
        article.setBody(body);
        article.setCreate_date(new Date());
        return articleDao.updateById(article);
    }

    /**
     * @Pram:a
     * @Retrrn:a 新增文章
     */

    @Override
    public Boolean insert(String title, String body) {
        Article article = new Article();
        article.setTitle(title);
        article.setBody(body);
        article.setCreate_date(new Date());
        return articleService.save(article);
    }
}
